﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_Category_Create
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and function"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hidID.Value = IIf(Request.QueryString("CatId") = "", 0, Request.QueryString("CatId"))
        End If
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Dim ParentCatId As Integer = hidID.Value ' IIf(Request.QueryString("CatId") = "", 0, Request.QueryString("CatId"))
            Dim p As New VSModel.Category
            Try

                p.Title = txtTitle.Text.Trim()
                p.QueryString = makeURLFriendly(txtTitle.Text.Trim)
                p.StatusId = 1
                p.Created = Date.Now
                p.Creator = Session("UserName")
                p.QueryString = makeURLFriendly(txtTitle.Text.Trim)
                p.ParentId = CInt(ParentCatId)
                p.Description = txtDescription.Text.Trim
                Dim parent As Integer = CInt(ParentCatId)
                p.TitleEn = txtTitleEn.Text
                p.LanguageId = Session("LanguageId")
                Dim t = (From h In data.Categories Where h.ParentId = parent Order By h.Sort Descending Select h).FirstOrDefault
                If Not t Is Nothing Then
                    p.Sort = CInt(t.Sort.ToString) + 1
                Else
                    p.Sort = 1
                End If
                data.Categories.AddObject(p)
                data.SaveChanges()
                Insert_App_Log("Insert Category:" & txtTitle.Text.Trim & "", Function_Name.Category, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))

                Excute_Javascript("Alertbox('Thêm mới dữ liệu thành công.');window.location ='../../Page/Category/List.aspx?CatId=" & Request("CatId") & "';", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub

    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
#End Region

   
End Class
